//
//  BGMShareClass.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/16.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "BGMShareClass.h"

#import "SVProgressHUD.h"
#import "BGMUser.h"
#import "BGMHTTP.h"
#import "Common.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
@implementation BGMShareClass

- (void)sharetype:(int)type publishId:(NSString *)publishId pusblishUserId:(NSString *)pusblishUserId mobShareTitle:(NSString *)mobShareTitle{
    
    switch (type) {
     
        case 102:
        {
            
//            BOOL wechat=[[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"mqqapi://"]];
//            if(wechat){
              [self forwardPublishType:@"3" publishId:publishId pusblishUserId:pusblishUserId mobShareTitle:mobShareTitle];
//            }
//            else{
//                [SVProgressHUD showInfoWithStatus:@"应用未安装"];
//            }
            
         
      
        }
            break;
        case 103:
        {
            //微博
        }
            break;
            
        default:
            break;
    }
}

- (void)forwardPublishType:(NSString *)type publishId:(NSString *)publishId pusblishUserId:(NSString *)pusblishUserId mobShareTitle:(NSString *)mobShareTitle{
    
    
    
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,
                             @"helpId":[BGMUser shareInstance].userId,
                             @"publishId":publishId,
                             @"userId":pusblishUserId,
                             @"helpType":type
                             };
    [BGMHTTP requestWithMethodName:@"index/helpPublish" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
            if ([code isEqualToString:@"200"]) {
                if ([type isEqualToString:@"1"]) {
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"shareAppSucess" object:nil];
                    [SVProgressHUD showSuccessWithStatus:@"转发成功~"];
                }else if ([type isEqualToString:@"3"]){
                    [self shareWXFriendpublishId:publishId pusblishUserId:[BGMUser shareInstance].userId mobShareTitle:mobShareTitle];
                    [SVProgressHUD dismiss];
                }else if ([type isEqualToString:@"2"]){
                    [SVProgressHUD showSuccessWithStatus:@"转发成功~"];
                }
            }
        });
    }];
    
}


- (void)shareWXFriendpublishId:(NSString *)publishId pusblishUserId:(NSString *)pusblishUserId mobShareTitle:(NSString *)mobShareTitle{
    
    NSString *str = [NSString stringWithFormat:@"%@detail.html?helpUserId=%@&publishId=%@&helpType=3",Server_url,pusblishUserId,publishId];
    NSURL *url = [NSURL URLWithString:str];
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:@"分享内容"
                                     images:[UIImage imageNamed:@"normalicon"] //传入要分享的图片
                                        url:url
                                      title:mobShareTitle
                                       type:SSDKContentTypeAuto];
    
    //进行分享
    [ShareSDK share:SSDKPlatformSubTypeWechatTimeline //传入分享的平台类型
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { // 回调处理....
         switch (state) {
             case SSDKResponseStateUpload:
                 // 分享视频的时候上传回调，进度信息在 userData
                 break;
             case SSDKResponseStateSuccess:
             {
                 
                 
             }
                 
                 break;
             case SSDKResponseStateFail:
             {
                 NSLog(@"--%@",error.description);
                 //失败
                 break;
             }
             case SSDKResponseStateCancel:
             {
                 [SVProgressHUD showInfoWithStatus:@"取消分享"];
             }
                 //取消
                 break;
                 
             default:
                 break;
         }
         
     }];
    
    
}

@end
